var attributeOptions = {
	jsonResult : {},
	hideOptions : ['general_description', 'image', 'mail', 'phone', 'webpage', 'twitter', 'facebook', 'rank'],
	convertData : function () {
		//go ever the data and identify the attributes and alterantives
		this.attributesList = [];
		this.attributesOptions = {};
		for (var i=0; i<data.length; i++) {
			var obj = data[i];
			//what atributes 
			for (var attr in obj) {
				if(this.attributesList.indexOf(attr)==-1){
					this.attributesList.push(attr);
					this.attributesOptions[attr] = [];
				}
				if(this.attributesOptions[attr].indexOf(obj[attr])==-1 && $.trim(obj[attr])!==''){
					this.attributesOptions[attr].push(obj[attr]);
				}
			}
		}
		
	},

	createRow : function (id) {
		//create a row per attribute
		for (var i=0; i<this.attributesList.length; i++) {
			var attrID = this.attributesList[i];
			//selector will be either a drop menu or a free text field
			if(typeof(this.attributesOptions[attrID])!=='undefined')
				{
				//create a drop down menu
				var listOptions = this.attributesOptions[attrID];
				var selector = $(document.createElement('select')).
					attr('id', 'select_'+attrID).attr('content', attrID).
					attr('size', '4').attr('style', 'word-wrap:break-word;width:100%;').attr('multiple', 'on');
				var optList = this.attributesOptions[attrID].sort();
				for (var j=0; j<optList.length; j++) {
					var optID = optList[j];
					selector.
						append('<option value="'+optID+'">'+optID+'</option>')
				}
			}
			else 
				{
				var selector = '<input type="text" class="search" id="'+attrID+'_selector" />';
			}
			$('#'+id).append(
				$('<tr id="tr_'+attrID+'"></tr>').
					append(
						$(document.createElement('td')).
							append('<input type="checkbox" id="'+attrID+'_check" content="'+attrID+'" checked/><label for="'+attrID+'_check">'+attrID+'</label>')
						).
					append(
						$(document.createElement('td')).
							append(selector)
						)
				)

			

		}
		//add button
		$('#'+id).append('<input type="submit" value="Show Candidates!" onClick="attributeOptions.buildJSON()"/>')
	//add jqueryui effects
	$("#optionsTable").buttonset();
	//hide options that users should not select
	for (var i=0; i<this.hideOptions.length; i++) {
		var attrID = this.hideOptions[i];
		$('#tr_'+attrID).hide();
	}
	
	},
	buildJSON : function () {
		this.resultJSON = {};this.resultJSON.candidates = [];
		//What has been selected to display?
		var checkBoxes = $(':checkbox:checked');
		
		attrSelected = [];
		
		for (var i=0; i<checkBoxes.length; i++) {
			var attrID = $(checkBoxes[i]).attr('content');
			attrSelected.push(attrID);
		}

		//Now for the selected data
		//now find, in the dataset, the items that match the selections
		var selections = $('select');
		for (var i=0; i<selections.length; i++) {
			var vals2rim = $(selections[i]).val();
			var attrID = $(selections[i]).attr('content');
			if(vals2rim!==null){
				for (var ii=0; ii<data.length; ii++) {
					var obj = data[ii];
					
					for (var j=0; j<vals2rim.length; j++) {
						if(typeof(obj[attrID])!=='undefined' && obj[attrID].match(vals2rim[j])){
							//add this candidate to the rsult, but only the attribute that were chosen to display
							var tmp = {};
							for (var k=0; k<attrSelected.length; k++) {
								if(typeof(obj[attrSelected[k]])!=='undefined'){
									tmp[attrSelected[k]] = obj[attrSelected[k]];
								}
							}
							this.resultJSON.candidates.push(tmp);
						}
					}
				}
			}
		}
	jsonResult = this.resultJSON;
	displayResults(this.resultJSON);	
	}

	
	
}